#include <iostream>
#include <cstring>
using namespace std;
// 用于计算字符串中的替换字符个数
int special_char(char * s,int n,char ch = ' ')
{
    int spaci_num = 0;
    for(int i=0;i<n;i++)
    {
        if(s[i] == ch)
        {
            spaci_num++;
        }
    }
    return  spaci_num;
}
// 替换字符为sp字符串,sp_num是替换字符串的元素个数
void ReplaceBlank(char *s,int length,char *sp,int sp_num)
{
    int p2 = length+(sp_num-1)*special_char(s,length);
//        int sp_num = sizeof(sp_num);
    while (length!=p2&&length>=0&&p2>=0)
    {
        while(s[length]!=' ')
        {
            s[p2] = s[length];
            length--;
            p2--;
        }
        for(int i=sp_num-1;i>=0;i--)
        {
            s[p2] = sp[i];
            p2--;
        }
        length--;
    }
}
// 用于显示字符串，方便调试
void show_cstr(char *s,int n)
{
    for(int i=0;i<n;i++)
        cout<<s[i];
    cout<<"\n";
}
int main() {
//    char s[30] = {'W','e',' ','A','r','e',' ','H','a','p','p','y','.','H','a','H','a',' ',' '};
    char s[] = {'W','e',' ','A','r','e',' ','H','a','p','p','y','.'};
    char sptial[] = {'%','2','0'};
    int p1=strlen(s);
    int space_num = special_char(s,p1);
    cout<<"替换后的字符串为： "<<endl;
    ReplaceBlank(s,p1,sptial,sizeof(sptial));
    show_cstr(s,strlen(s));
    return 0;
}
